﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LuceneSearcher;

namespace WebSearcher
{
    public partial class Results : System.Web.UI.Page
    {
        MainSearcher searcher = new MainSearcher();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                // 如果不是回传动作，则接收get请求参数
                string keyword = this.txtKeyword.Text.Trim();
                if (string.IsNullOrEmpty(keyword))
                {
                    keyword = Request.Params["keyword"];
                }
                this.Search(keyword);
            } else
            {
                string keyword = this.txtKeyword.Text.Trim();
                this.Search(keyword);              
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string keyword = this.txtKeyword.Text.Trim();
            this.Search(keyword);
        }

        /// <summary>
        /// 搜索指定的关键字，并输出结果
        /// </summary>
        /// <param name="keyword"></param>
        private void Search(string keyword)
        {
            if (!string.IsNullOrEmpty(keyword))
            {
                keyword = keyword.Trim();
                // 请求设置了keyword查询请求
                string dirIndex = Server.MapPath("./index");
                this.lstReapter.DataSource = searcher.Search(dirIndex, keyword);
                this.lstReapter.DataBind();
                this.lblCount.Text = searcher.Count.ToString();
                this.lblTime.Text = searcher.ElapsedTime.ToString();

                this.txtKeyword.Text = keyword;
            }
            else
            {
                // 请求未设置keyword查询请求
                return;
            }
        }
    }
}
